package com.ymatou.iapi.cardservice.testcase;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.cardservice.parameter.CardInfo;
import com.ymatou.iapi.cardservice.parameter.StatusCodeEnum;
import com.ymatou.iapi.cardservice.parameter.UserCardModel;
import com.ymatou.iapi.cardservice.service.BindingCardCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.CardCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.cardservice.CardServiceMapper;

public class Ts_BindingCard {
	
	private static BindingCardCall bindingCardCall = new BindingCardCall();
	private static CardServiceMapper cardServiceMapper = SqlMapperFactory.getCardServiceMapper();
	private static String cardId = CardInfo.CardId1;
	private static String cardId_pswd = CardInfo.CardId1_Pswd;
	private static int userId = CardInfo.userId;
	private static String userName = CardInfo.userName;
	
	
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("用户绑定卡");
	}
	
	@Before
	public void caseUp() {
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(bindingCardCall.getOpurl());
	}
	
	@After
	public void caseDown() {
		Logger.end(); 
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindingCard_001() throws Exception {
		Logger.start(true,"用户绑定卡");
		try{
			//解绑卡
			cardServiceMapper.updateCardAsOrigByCardId(cardId);

			int bindCnt = (int)cardServiceMapper.selectCardBatchByCardId(cardId).get("BindingCount");
			
			JSONObject ret = bindingCardCall.call(cardId_pswd, userId, userName);
			
			
			//check response json
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			CardCallService.CheckUserCardModel(ret.getJSONObject("data"), cardId);
			
			//check db
			checkBindingCardInDB(cardId,userId,userName,5,bindCnt+1);
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindingCard_002() throws Exception {
		Logger.start(true,"参数为空-AppId, cardpwd,userId,userName");
		try{
			//解绑卡
			cardServiceMapper.updateCardAsOrigByCardId(cardId);

			int bindCnt = (int)cardServiceMapper.selectCardBatchByCardId(cardId).get("BindingCount");
			
			//appid 为空
			JSONObject ret = bindingCardCall.call(cardId_pswd, userId, userName,"");
			Logger.verifyEquals(StatusCodeEnum.ArgumentException.getCode(), ret.getInt("statusCode"), "check statusCode");
			checkFailedToBingCard(cardId,bindCnt);
			
			//cardpwd 为空
			ret = bindingCardCall.call("", userId, userName);
			Logger.verifyEquals(StatusCodeEnum.ArgumentException.getCode(), ret.getInt("statusCode"), "check statusCode");
			checkFailedToBingCard(cardId,bindCnt);
			
			
			//userId 为空
			ret = bindingCardCall.call(cardId_pswd, 0, userName);
			Logger.verifyEquals(StatusCodeEnum.ArgumentException.getCode(), ret.getInt("statusCode"), "check statusCode");
			checkFailedToBingCard(cardId,bindCnt);
			
			//userName 为空
			ret = bindingCardCall.call(cardId_pswd, userId, "");
			Logger.verifyEquals(StatusCodeEnum.ArgumentException.getCode(), ret.getInt("statusCode"), "check statusCode");
			checkFailedToBingCard(cardId,bindCnt);
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindingCard_003() throws Exception {
		Logger.start(true,"密码对应卡不存在");
		try{
			//解绑卡
			cardServiceMapper.updateCardAsOrigByCardId(cardId);

			int bindCnt = (int)cardServiceMapper.selectCardBatchByCardId(cardId).get("BindingCount");
			
			JSONObject ret = bindingCardCall.call(cardId_pswd+"1", userId, userName);
			
			
			//check response json
			Logger.verifyEquals(StatusCodeEnum.CardNotExit.getCode(), ret.getInt("statusCode"), "check statusCode");
			
			//check db
			checkFailedToBingCard(cardId,bindCnt);
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindingCard_004() throws Exception {
		Logger.start(true,"绑定已绑定卡");
		try{
			cardServiceMapper.updateCardAsOrigByCardId(cardId);

			int bindCnt = (int)cardServiceMapper.selectCardBatchByCardId(cardId).get("BindingCount");
			
			JSONObject ret = bindingCardCall.call(cardId_pswd, userId, userName);
			
			
			//check response json
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			CardCallService.CheckUserCardModel(ret.getJSONObject("data"), cardId);
			
			//check db
			checkBindingCardInDB(cardId,userId,userName,5,bindCnt+1);
			
			
			//再次绑定
			ret = bindingCardCall.call(cardId_pswd, userId, userName);
			
			//check response json
			Logger.verifyEquals(StatusCodeEnum.CardIsBinding.getCode(), ret.getInt("statusCode"), "check statusCode");
			
			//check db
			checkBindingCardInDB(cardId,userId,userName,5,bindCnt+1);
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindingCard_005() throws Exception {
		Logger.start(true,"绑定未激活卡");
		try{
			//解绑卡
			cardServiceMapper.updateCardStatusByCardId(cardId,0);

			int bindCnt = (int)cardServiceMapper.selectCardBatchByCardId(cardId).get("BindingCount");
			
			JSONObject ret = bindingCardCall.call(cardId_pswd, userId, userName);
			
			
			//check response json
			Logger.verifyEquals(StatusCodeEnum.CardUnactivated.getCode(), ret.getInt("statusCode"), "check statusCode");
			
			//check db
			checkBindingCardInDB(cardId,0,"",0,bindCnt);
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	
	
	public static UserCardModel getUserCardModelFromDB(String cardId){
		Map<String,Object> cardMap = cardServiceMapper.selectCardByCardId(cardId);
		UserCardModel userCardModel = new UserCardModel();
		userCardModel.setCardId(cardId);
		userCardModel.setAvailAmount(new BigDecimal(String.valueOf(cardMap.get("AvailAmount"))));
		userCardModel.setDenomination(new BigDecimal(String.valueOf(cardMap.get("Denomination"))));
		userCardModel.setRemark(String.valueOf(cardMap.get("Remark")));
		userCardModel.setBeginTime((Date)cardMap.get("BeginTime"));
		userCardModel.setEndTime((Date)cardMap.get("EndTime"));
		userCardModel.setCardType((int)cardMap.get("CardType"));
		return userCardModel;
	}
	
	
	public static void checkFailedToBingCard(String cardId,int bindCnt){
		checkBindingCardInDB(cardId,0,"",4,bindCnt);
	}
	
	public static void checkBindingCardInDB(String cardId,int userId,String userName,int status, int bindCnt){
		Map<String,Object> cardMap = cardServiceMapper.selectCardByCardId(cardId);
		Map<String,Object> cardBatchMap = cardServiceMapper.selectCardBatchByCardId(cardId);
		
		Logger.verifyEquals(userId, (int)cardMap.get("UserId"), "check card userId");
		Logger.verifyEquals(userName, String.valueOf(cardMap.get("UserName")), "check card UserName");
		Logger.verifyEquals(status, (int)cardMap.get("Status"), "check card status");
		Logger.verifyEquals(bindCnt, (int)cardBatchMap.get("BindingCount"), "check card binding count");
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
